package com.gadgets.cm04.fileregix.start;

import com.gadgets.cm04.common.GadgetsCm04Constant;
import com.gadgets.cm04.fileregix.dto.FileRegixQueryDTO;
import com.gadgets.cm04.fileregix.pojo.FileRegixInfo;
import com.gadgets.cm04.fileregix.repository.IFileRegixInfoRepository;
import com.gadgets.framework.common.util.CacheUtils;
import com.gadgets.framework.start.AbsCacheInitLoaderProcess;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author UncleXiu
 * @version V1.0.0
 * @data 2020-10-19
 * 
 */
@Component
public class FileRegixInitLoadProcess extends AbsCacheInitLoaderProcess {

    @Autowired
    private IFileRegixInfoRepository fileRegixInfoRepository;
    @Autowired
    private CacheUtils cacheUtils;

    public FileRegixInitLoadProcess() {
        super("5");
    }

    /**
     * 初始化执行方法
     */
    @Override
    public void initLoad() {
        logger.info("开始加载文件上传/下载规则");
        FileRegixQueryDTO queryDTO = new FileRegixQueryDTO();
        List<FileRegixInfo> fileRegixInfos = fileRegixInfoRepository.queryFileRegixInfo(queryDTO);
        List<FileRegixInfo> uploadRegix = new ArrayList<>();
        List<FileRegixInfo> downRegix = new ArrayList<>();
        if(null!=fileRegixInfos && fileRegixInfos.size()>0){
            for (FileRegixInfo fileRegixInfo : fileRegixInfos) {
                if(GadgetsCm04Constant.REGIX_TYPE_CONSTANT.REGIX_UPLOAD_WHITE.equals(fileRegixInfo.getRegixType())
                ||GadgetsCm04Constant.REGIX_TYPE_CONSTANT.REGIX_UPLOAD_BLACK.equals(fileRegixInfo.getRegixType())){
                    uploadRegix.add(fileRegixInfo);
                }else {
                    downRegix.add(fileRegixInfo);
                }
            }
        }
        // 启动阶段将文件规则加入到缓存当中
        cacheUtils.putDefualtCache(GadgetsCm04Constant.FILE_UPLOAD_CACHE_KEY,uploadRegix);
        cacheUtils.putDefualtCache(GadgetsCm04Constant.FILE_DOWNLOAD_CACHE_KEY,downRegix);
        logger.info("加载文件上传/下载规则完成");
    }

}
